package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.guide_service;
import entity.*;

public class edit_guide_servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private guide_service gd_service=new guide_service();
	public edit_guide_servlet() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		HttpSession session= request.getSession();
		String requestURI = request.getRequestURI();
		user u=(user)session.getAttribute("user");
		String userAgent = request.getHeader("User-Agent");
		boolean is_mobile = false;
		String[] mobileKeywords = {"Android", "iPhone", "iPad", "Windows Phone"};
		for (String keyword : mobileKeywords) {
		    if (userAgent.contains(keyword)) {
		    	is_mobile = true;
		        break;
		    }
		}
		request.setAttribute("is_mobile", is_mobile);
		if (requestURI.equals("/teamfight_wiki/hero_guide_edit")){
			if (u==null || u.getType()!=2){
				request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
				return;
			}
			String hero_id=request.getParameter("id");
			List<hero>target=gd_service.get_hero_by_id(hero_id);
			request.setAttribute("heros", target);
			request.getRequestDispatcher("/guide/hero_guide_edit.jsp").forward(request,response);
		}
		if(requestURI.equals("/teamfight_wiki/hero_guide_edit_msg")){
			int hero_id=Integer.parseInt(request.getParameter("id"));	//   ģ    Ǵ     
			String hero_name=request.getParameter("name");
			String hero_img=request.getParameter("img_url");	//   ģ    Ǵ     
			String hero_attribute=request.getParameter("attribute");
			int hero_cost=Integer.parseInt(request.getParameter("cost"));
			String hero_skill=request.getParameter("skill");
			String hero_trammel=request.getParameter("trammel");//   ģ    Ǵ     
			String hero_trammel_id=request.getParameter("trammel_id");//   ģ    Ǵ     
			hero target=new hero(hero_id,hero_name,hero_attribute,hero_cost,hero_skill,hero_trammel,hero_trammel_id,hero_img);
			gd_service.edit_hero(target);
			request.getRequestDispatcher("/hero_guide").forward(request,response);
			return;
		}
		
		
		
		if (requestURI.equals("/teamfight_wiki/trammel_guide_edit")){
			if (u==null || u.getType()!=2){
				request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
				return;
			}
			String trammel_id=request.getParameter("id");
			List<trammel>target=gd_service.get_trammel_by_id(trammel_id);
			request.setAttribute("trammels", target);
			request.getRequestDispatcher("/guide/trammel_guide_edit.jsp").forward(request,response);
		}
		if(requestURI.equals("/teamfight_wiki/trammel_guide_edit_msg")){
			int trammel_id=Integer.parseInt(request.getParameter("id"));	//   ģ    Ǵ     
			String trammel_name=request.getParameter("name");
			String trammel_img=request.getParameter("img_url");	//   ģ    Ǵ     
			String trammel_description=request.getParameter("description");
			String trammel_number=request.getParameter("number");
			String trammel_hero=request.getParameter("trammel_hero");//   ģ    Ǵ     
			String trammel_hero_id=request.getParameter("trammel_hero_id");//   ģ    Ǵ     
			trammel target=new trammel(trammel_id,trammel_name,trammel_description,trammel_number,trammel_hero,trammel_hero_id, trammel_img);
			gd_service.edit_trammel(target);
			request.getRequestDispatcher("/trammel_guide").forward(request,response);
			return;
		}
		
		
		if (requestURI.equals("/teamfight_wiki/hex_guide_edit")){
			if (u==null || u.getType()!=2){
				request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
				return;
			}
			String hex_id=request.getParameter("id");
			List<hex>target=gd_service.get_hex_by_id(hex_id);
			request.setAttribute("hexs", target);
			request.getRequestDispatcher("/guide/hex_guide_edit.jsp").forward(request,response);
		}
		if(requestURI.equals("/teamfight_wiki/hex_guide_edit_msg")){
			int hex_id=Integer.parseInt(request.getParameter("id"));	//   ģ    Ǵ     
			String hex_name=request.getParameter("name");
			String hex_img=request.getParameter("img_url");	//   ģ    Ǵ     
			String hex_description=request.getParameter("description");
			String hex_type=request.getParameter("type");
			hex target=new hex(hex_id,hex_name,hex_description,hex_img,hex_type);
			gd_service.edit_hex(target);
			request.getRequestDispatcher("/hex_guide").forward(request,response);
			return;
		}
		
		
		if (requestURI.equals("/teamfight_wiki/equip_guide_edit")){
			if (u==null || u.getType()!=2){
				request.getRequestDispatcher("/account_manage/home.jsp").forward(request,response);
				return;
			}
			String equip_id=request.getParameter("id");
			List<equip>target=gd_service.get_equip_by_id(equip_id);
			request.setAttribute("equips", target);
			request.getRequestDispatcher("/guide/equip_guide_edit.jsp").forward(request,response);
		}
		if(requestURI.equals("/teamfight_wiki/equip_guide_edit_msg")){
			int equip_id=Integer.parseInt(request.getParameter("id"));	//   ģ    Ǵ     
			String equip_name=request.getParameter("name");
			String equip_img=request.getParameter("img_url");	//   ģ    Ǵ     
			String equip_description=request.getParameter("description");
			String sub_equip=request.getParameter("sub_equip");//   ģ    Ǵ     
			String sub_equip_id=request.getParameter("sub_equip_id");//   ģ    Ǵ     
			String equip_type=request.getParameter("equip_type");
			equip target=new equip(equip_id,equip_name,equip_description,sub_equip,sub_equip_id,equip_img,equip_type);
			gd_service.edit_equip(target);
			request.getRequestDispatcher("/equip_guide").forward(request,response);
			return;
		}
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
